package com.ruoyi.web.controller.business.sundry;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.sundry.domain.JbCheats;
import com.ruoyi.business.sundry.service.JbCheatsService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.sundry.domain.JbCheatsCategory;
import com.ruoyi.business.sundry.service.JbCheatsCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 秘籍分类 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("秘籍分类")
@RestController
@RequestMapping("/cheats_category")
public class JbCheatsCategoryController extends BaseController
{
    @Autowired
    private JbCheatsCategoryService service;
    @Autowired
    private JbCheatsService jbCheatsService;

    /**
     * 查询秘籍分类列表
     */
    @ApiOperation("查询秘籍分类列表")
    @PreAuthorize("@ss.hasPermi('cheats_category:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbCheatsCategory jbCheatsCategory)
    {
        LambdaQueryWrapper<JbCheatsCategory> select = new LambdaQueryWrapper<>();
        select.and(StringUtils.isNotBlank(page.getKeyword()),a ->a.like(JbCheatsCategory::getCategoryName,
                        page.getKeyword()).or()
                .like(JbCheatsCategory::getRemark,
                        page.getKeyword()));
        IPage<JbCheatsCategory> iPage = service.page(page.b(), select);
        return JoR.getDataTable(iPage);
    }

    /**
     * 导出秘籍分类列表
     */
    @ApiOperation("导出秘籍分类列表")
    @PreAuthorize("@ss.hasPermi('cheats_category:export')")
    @Log(title = "秘籍分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbCheatsCategory jbCheatsCategory)
    {
        LambdaQueryWrapper<JbCheatsCategory> select = new LambdaQueryWrapper<>();
        IPage<JbCheatsCategory> iPage = service.page(page.b(), select);

        List<JbCheatsCategory> list = iPage.getRecords();
        ExcelUtil<JbCheatsCategory> util = new ExcelUtil<JbCheatsCategory>(JbCheatsCategory.class);
        util.exportExcel(response, list, "秘籍分类数据");
    }

    /**
     * 获取秘籍分类详细信息
     */
    @ApiOperation("获取秘籍分类详细信息")
    @PreAuthorize("@ss.hasPermi('cheats_category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(service.getById(id));
    }

    /**
     * 新增秘籍分类
     */
    @ApiOperation("新增秘籍分类")
    @PreAuthorize("@ss.hasPermi('cheats_category:add')")
    @Log(title = "秘籍分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbCheatsCategory jbCheatsCategory)
    {
        return toAjax(service.save(jbCheatsCategory));
    }

    /**
     * 修改秘籍分类
     */
    @ApiOperation("修改秘籍分类")
    @PreAuthorize("@ss.hasPermi('cheats_category:edit')")
    @Log(title = "秘籍分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbCheatsCategory jbCheatsCategory)
    {
        return toAjax(service.updateById(jbCheatsCategory));
    }

    /**
     * 删除秘籍分类
     */
    @ApiOperation("删除秘籍分类")
    @PreAuthorize("@ss.hasPermi('cheats_category:remove')")
    @Log(title = "秘籍分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(service.removeByIds(Arrays.asList(ids)));
    }
    /**
     * 查询秘籍分类列表
     */
    @ApiOperation("查询秘籍分类列表")
    @PreAuthorize("@ss.hasPermi('cheats_category:list')")
    @GetMapping("/listData")
    public AjaxResult listData(JbCheatsCategory jbCheatsCategory)
    {
        LambdaQueryWrapper<JbCheatsCategory> select = new LambdaQueryWrapper<>();
        select.and(StringUtils.isNotBlank(jbCheatsCategory.getCategoryName()),a ->a.like(JbCheatsCategory::getCategoryName,
                        jbCheatsCategory.getCategoryName()).or()
                .like(JbCheatsCategory::getRemark,
                        jbCheatsCategory.getCategoryName()));
        return success(service.list(select));
    }
}
